package com.github.aloxc.plugin.restplus.action;


import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.github.aloxc.plugin.restplus.common.PsiClassHelper;
import com.github.aloxc.plugin.DonotGuard;
import com.github.aloxc.plugin.restplus.popup.PopupBalloon;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.asJava.LightClassUtil;
import org.jetbrains.kotlin.asJava.LightClassUtilsKt;
import org.jetbrains.kotlin.psi.KtClassOrObject;

import java.awt.datatransfer.StringSelection;
/**
 *
 * @author leero
 */
@DonotGuard
public class ConvertClassToJsonAction extends AbstractBaseAction {
    @Override
    public void actionPerformed(AnActionEvent e) {
        Editor myEditor = e.getData(CommonDataKeys.EDITOR);

        PsiElement psiElement = e.getData(CommonDataKeys.PSI_ELEMENT);
        PsiClass psiClass = getPsiClass(psiElement);

        if (psiClass == null) {
            return;
        }

        String json = PsiClassHelper.create(psiClass).convertClassToJson(myProject(e), true);
        CopyPasteManager.getInstance().setContents(new StringSelection(json));

        PopupBalloon.showPopupBalloon(myEditor, String.format(I18Bundle.getMessage("com.github.aloxc.plugin.restplus.copy.beanjson.text"), json), false);
    }

    @Nullable
    protected PsiClass getPsiClass(PsiElement psiElement) {
        PsiClass psiClass = null;
        if (psiElement instanceof PsiClass) {
            psiClass = (PsiClass) psiElement;

        } else if (psiElement instanceof KtClassOrObject) {
            if (LightClassUtil.INSTANCE.canGenerateLightClass((KtClassOrObject) psiElement)) {
                psiClass = LightClassUtilsKt.toLightClass((KtClassOrObject) psiElement);
            }
        }
        return psiClass;
    }

    @Override
    public void update(AnActionEvent e) {
        PsiElement psiElement = e.getData(CommonDataKeys.PSI_ELEMENT);
        boolean visible = psiElement instanceof PsiClass /*|| psiElement instanceof KtClassOrObject*/;
        if (visible) {
            e.getPresentation().setText(
                    isCompressed() ?
                            I18Bundle.getMessage("com.github.aloxc.plugin.restplus.copy.beanjsonCompressed.edit.text") :
                            I18Bundle.getMessage("com.github.aloxc.plugin.restplus.copy.beanjson.edit.text")

            );

            e.getPresentation().setDescription(
                    isCompressed() ?
                            I18Bundle.getMessage("com.github.aloxc.plugin.restplus.copy.beanjsonCompressed.edit.text") :
                            I18Bundle.getMessage("com.github.aloxc.plugin.restplus.copy.beanjson.edit.text")

            );
        }
        setActionPresentationVisible(e, visible);
    }

    public boolean isCompressed() {
        return false;
    }
}
